Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Google Medium
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Google Hard
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Burst Balloons

Problem Statement

You are given a row of n balloons, each marked with a specific number i.e. ith ballon is marked with nums[i], wheres nums is an integer array. You are asked to burst all the balloons.

When you burst the ith balloon, you get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 indexes are out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

Return the highest possible coin total after popping all the balloons.

Examples

  • Example 1:

    • Input: [3, 1, 5]
    • Expected Output: 35
    • Justification: Popping balloon 1 (value 1) first earns 3*1*5=15 coins, leaving [3, 5].Next, pop 3 to earn 1*3*5=15, and finally pop 5 to earn 1*5*1=5, for a total of 15+15+5=35 coins.
  • Example 2:

    • Input: [1, 5, 8]
    • Expected Output: 56
    • Justification: First, pop balloon 1 (value 5), earning 1*5*8=40 coins, leaving [1, 8]. Then, pop balloons 0 and 2, earning 1*1*8=8 and 1*8*1=8 coins respectively, for a total of 40+8+8=56 coins.
  • Example 3:

    • Input: [2, 4, 3, 5]
    • Expected Output: 115
    • Justification: A strategic approach yields the maximum coins: start with balloon 2 (value 3) for 4*3*5=60, then balloon 2 (value 4) for 2*4*5=40, followed by the outer balloons, 1*2*5=10 and 1*5*1=5, summing up to 24+30+10+2=115 coins.

Solution

To solve this problem, a dynamic programming approach is most effective. The key idea is to consider popping balloons in reverse, i.e., imagine adding a balloon back in its place and determining the maximum coins that could be earned up to that point. This reverse thinking helps in simplifying the problem by breaking it down into smaller subproblems, where the solution to each subproblem builds up to the final solution. This method ensures that all potential combinations of bursts are considered without redundant calculations, optimizing both time and space efficiency.

Initially, we approach the problem by considering all possible segments of balloons and determining the maximum coins that can be obtained for each segment. By iteratively expanding the size of these segments and calculating the maximum coins for each, utilizing previously solved smaller segments, we build up to the solution for the entire set of balloons. This bottom-up approach minimizes recalculations and exploits the problem's optimal substructure, making it a fitting strategy for achieving the goal.

Step-by-Step Algorithm

  1. Initialize the DP Table:

    • Create a 2-dimensional array dp, with dimensions (n+2) x (n+2), where n is the number of balloons. This table will hold the maximum coins that can be obtained for bursting all balloons in the subarray defined by its indices. Initialize all cells with 0.
  2. Prepare the Input Array:

    • Construct a new array ballons by adding two extra elements, both 1s, to the beginning and end of the original array nums. This accounts for the edge case where bursting a balloon on one end multiplies its value by 1.
  3. Populate the DP Table:

    • For each possible length of subarray from 1 to n:
      • For each possible starting index left of the subarray:
        • Calculate the ending index right of the subarray based on its length.
        • For each possible position i of the last balloon to burst in the subarray:
          • Compute the coins earned by bursting the ith balloon last. This includes the coins from the burst itself, calculated as ballons[left-1] * ballons[i] * ballons[right+1], plus the maximum coins obtainable from the subarrays to the left and right of i, already stored in dp[left][i-1] and dp[i+1][right].
          • Update dp[left][right] with the maximum of its current value and the newly calculated coins.
  4. Return the Result:

    • The maximum coins obtainable by bursting all balloons is stored in dp[1][n], accounting for the added padding. Return this value as the result.

Algorithm Walkthrough

Let's consider the input: nums = [2, 4, 3, 5]

Initial Setup

  • Extended Array: newNums = [1, 2, 4, 3, 5, 1]
  • DP Table Initialization: A 6x6 matrix filled with zeros.

Iteration Through Subarrays

The process iterates through subarrays of increasing length, calculating the maximum coins that can be obtained by bursting all balloons in each subarray.

Length 1 Subarrays

  • Subarray [2]: (left = 1, right = 1, i = 1)
    • coins = 1*2*4 + 0 + 0 = 8
    • dp[1][1] = 8
  • Subarray [4]: (left = 2, right = 2, i = 2)
    • coins = 2*4*3 + 0 + 0 = 24
    • dp[2][2] = 24
  • Subarray [3]: (left = 3, right = 3, i = 3)
    • coins = 4*3*5 + 0 + 0 = 60
    • dp[3][3] = 60
  • Subarray [5]: (left = 4, right = 4, i = 4)
    • coins = 3*5*1 + 0 + 0 = 15
    • dp[4][4] = 15
[
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 8, 0, 0, 0, 0 ],
  [ 0, 0, 24, 0, 0, 0 ],
  [ 0, 0, 0, 60, 0, 0 ],
  [ 0, 0, 0, 0, 15, 0 ],
  [ 0, 0, 0, 0, 0, 0 ]
]

Length 2 Subarrays

  • Subarray [2, 4]:
    • When i = 1 (bursting 2 last): coins = 1*2*3 + 0 + 24 = 30
    • When i = 2 (bursting 4 last): coins = 1*4*3 + 8 + 0 = 20
    • dp[1][2] = 30 (the maximum of the two)
  • Subarray [4, 3]:
    • When i = 2: coins = 2*4*5 + 0 + 60 = 100
    • When i = 3: coins = 2*3*5 + 24 + 0 = 54
    • dp[2][3] = 100
  • Subarray [3, 5]:
    • When i = 3: coins = 4*3*1 + 0 + 15 = 27
    • When i = 4: coins = 4*5*1 + 60 + 0 = 80
    • dp[3][4] = 80
[
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 8, 30, 0, 0, 0 ],
  [ 0, 0, 24, 100, 0, 0 ],
  [ 0, 0, 0, 60, 80, 0 ],
  [ 0, 0, 0, 0, 15, 0 ],
  [ 0, 0, 0, 0, 0, 0 ]
]

Length 3 Subarrays

  • Subarray [2, 4, 3]:
    • When i = 1 (bursting 2): coins = 1*2*5 + 0 + 100 = 110
    • When i = 2 (bursting 4): coins = 1*4*5 + 8 + 60 = 88
    • When i = 3 (bursting 3): coins = 1*3*5 + 24 + 0 = 39
    • dp[1][3] = 110
  • Subarray [4, 3, 5]:
    • Similar calculations yield the best outcome for this subarray.
[
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 8, 30, 110, 0, 0 ],
  [ 0, 0, 24, 100, 110, 0 ],
  [ 0, 0, 0, 60, 80, 0 ],
  [ 0, 0, 0, 0, 15, 0 ],
  [ 0, 0, 0, 0, 0, 0 ]
]

Full Array [2, 4, 3, 5]

  • Subarray [2, 4, 3, 5]:
    • When i = 1 (bursting 2): coins = 1*2*1 + 0 + 100 = 102
    • When i = 2 (bursting 4): coins = 1*4*1 + 8 + 60 = 72
    • When i = 3 (bursting 3): coins = 1*3*1 + 24 + 0 = 27
    • When i = 4 (bursting 3): coins = 1*5*1 + 110 + 0 = 115
    • dp[1][3] = 115
[
  [ 0, 0, 0, 0, 0, 0 ],
  [ 0, 8, 30, 110, 115, 0 ],
  [ 0, 0, 24, 100, 110, 0 ],
  [ 0, 0, 0, 60, 80, 0 ],
  [ 0, 0, 0, 0, 15, 0 ],
  [ 0, 0, 0, 0, 0, 0 ]
]

Final DP Table Update

  • The process iterates through all subarrays of all possible lengths, filling the DP table with the maximum coins that can be obtained for bursting all balloons in each subarray.
  • The final value, dp[1][4] = 115, gives the maximum coins for bursting all balloons in the array [2, 4, 3, 5].

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity of this algorithm is , where (n) is the number of balloons. This complexity arises because:

  1. We iterate over each possible subarray of balloons (n choices).
  2. For each subarray, we iterate over all possible positions of the last balloon to burst (n choices again).
  3. For each choice of the last balloon, we perform a calculation that includes accessing and updating entries in our dynamic programming table, which is itself indexed by two parameters (the left and right bounds of the subarray).

Therefore, the nested loops result in time complexity.

Space Complexity

The space complexity of the algorithm is due to the dynamic programming table (dp), which stores the maximum coins obtainable for every possible subarray of balloons.

Burst Balloons (hard)
Sum of Distances in Tree (hard)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Initial Setup

Iteration Through Subarrays

Final DP Table Update

Code

Complexity Analysis

Time Complexity

Space Complexity